var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var app = getApp();
Page({
  data: {
    corpuser: {
      id:0,
      userNo:'',
      userName: '',
      mobile: ''
    },
    corpuserId: 0
  },
  bindinputMobile(event) {
    let corpuser = this.data.corpuser; 
    corpuser.mobile = event.detail.value;
    this.setData({
      corpuser: corpuser
    });
  },
  bindinputUserName(event) {
    let corpuser = this.data.corpuser;
    corpuser.userName = event.detail.value;
    this.setData({
      corpuser: corpuser
    });
  },
  bindinputUserNo (event){
    let corpuser = this.data.corpuser;
    corpuser.userNo = event.detail.value;
    this.setData({
      corpuser: corpuser
    });
  },

  getCorpuserDetail() {
    let that = this;
    util.request(api.CorpUserDetail, { id: that.data.corpuserId }).then(function (res) {
      if (res.errno === 0) {
        if(res.data){
            that.setData({
              corpuser: res.data
            });
        }
      }
    });
  },


  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    if (options.id) {
      this.setData({
        corpuserId: options.id
      });
    }
    this.getCorpuserDetail();
  },

  onReady: function () {

  },
  cancelCorpuser(){
    wx.navigateBack({
      url: '/pages/ucenter/corpuser/corpuser',
    })
  },
  saveCorpuser(){
    let corpuser = this.data.corpuser;

    if (corpuser.userName == '') {
      util.showErrorToast('请输入姓名');

      return false;
    }

    if (corpuser.mobile == '') {
      util.showErrorToast('请输入手机号码');
      return false;
    }

    if (corpuser.userNo == 0) {
      util.showErrorToast('请输入员工编号');
      return false;
    }

    let that = this;
    util.request(api.CorpUserSave, { 
      userId: corpuser.id,
      userName: corpuser.userName,
      mobile: corpuser.mobile,
      userNo: corpuser.userNo,
    }, 'POST', 'application/json').then(function (res) {
      if (res.errno === 0) {
        wx.navigateBack({
          url: '/pages/ucenter/corpuser/corpuser',
        })
      }
    });

  },
  onShow: function () {
    // 页面显示

  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  }
})